$(function () {
    // 基于准备好的dom，初始化echarts实例
    let myChart = echarts.init(document.getElementById('main'));
    // 指定图表的配置项和数据
    let option = {
        title: {
            text: '工资曲线(单位：元)'
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    backgroundColor: '#6a7985'
                }
            }
        },
        legend: {
            data: ['工资','工资(含节日福利)']
        },
        xAxis: {
            data: [],
            axisLabel:{
                interval:0
            }
        },
        yAxis: {},
        series: [{
            name: '工资',
            type: 'line',
            data: []
        },{
            name: '工资(含节日福利)',
            type: 'line',
            data: []
        }]
    };
    $('#salaryAnalyze-dg').datagrid({
        striped: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        scrollbarSize: 0,
        width: "auto",
        showFooter: true,
        url: 'salaryAnalyze?method=findByUserId',
        onBeforeSelect: function () {
            return false;
        },
        columns: [[
            {field: 'issueYear', title: '年份', width: 15, align: 'center'},
            {field: 'salary', title: '工资合计', width: 20, align: 'right', halign: 'center'},
            {field: 'issueMonth', title: '统计月份数', width: 18, align: 'right', halign: 'center'},
            {field: 'avgSalary', title: '平均工资', width: 22, align: 'right', halign: 'center'},
            {field: 'benefits', title: '节日福利', width: 22, align: 'right', halign: 'center'},
            {field: 'salaryWithBenefits', title: '合计(含节日福利)', width: 25, align: 'right', halign: 'center'},
            {field: 'avgSalaryWithBenefits', title: '平均工资(含节日福利)', width: 30, align: 'right', halign: 'center'}
        ]],
        onLoadSuccess: function (data) {
            let footer = [];
            let salary =[];
            let salaryWithBenefits = [];
            if (data.rows.length > 6){
                option.xAxis.axisLabel.rotate = -20;
            }else{
                option.xAxis.axisLabel.rotate = 0;
            }
            $.each(data.rows, function (i, e) {
                footer.push(e.issueYear + "年 " + e.issueMonth + "个月工资");
                salary.push(e.salaryNum);
                option.xAxis.data = footer;
                option.series[0].data = salary;
                salaryWithBenefits.push(e.salaryNum + e.benefitsNum);
                option.series[1].data = salaryWithBenefits;
            });
            // 使用刚指定的配置项和数据显示图表。
            myChart.setOption(option);
        }
    });
});